<?php
/**
 * Created by PhpStorm.
 * User: 六花
 * Date: 2019/5/16
 * Time: 16:17
 */

class advertisement_settingApp extends BackendApp{


    var $_advertisement_mod;
    var $_theme_setting_mod;
    function __construct()
    {
        $this->advertisement_settingApp();
    }

    function advertisement_settingApp()
    {
        parent::BackendApp();
        $this->_advertisement_mod =& m('advertisement_setting');
        $this->_theme_setting_mod = &m("theme_setting");
    }

    function index(){
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        $conditions = 'advertisement_id >= 4';
        $page = $this->_get_page();
        $res = $this->_advertisement_mod->find(array(
            'conditions' => $conditions,
            'limit' => $page['limit'],
            'count' => true,
        )); //找出所有商城的合作伙伴
        $page['item_count'] = $this->_advertisement_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        foreach ($res as $key => &$ad) {
            $ad['advertisement_url'] && $res[$key]['advertisement_url'] = WEB_PHOTO_SRC . '/' . $ad['advertisement_url'];
            $ad['typeValue']     = Advertisement_settingModel::$typeValue[$ad['type']];
            $ad['typeIDValue']   = Advertisement_settingModel::$typePosition[$ad['type_id']];
            $ad['closeValue']    = Advertisement_settingModel::$closeValue[$ad['closed']];
            $ad['categoryValue'] = '首页主题';

            $themeInfo = $this->_theme_setting_mod->get(array("conditions"=>"theme_id = ".$ad['theme_id']));
            $ad['theme_title']   = $themeInfo['theme_title'];
        }
        $this->assign('res', $res);
        $this->assign('filtered', $filter? 1 : 0); //是否有查询条件
        $page['item_count'] = $this->_advertisement_mod->getCount();
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->display('advertisement_setting.index.html');
    }


    function add()
    {
        if (!IS_POST)
        {
            $res_theme = $this->_theme_setting_mod->find(array(
                "conditions" => "preview = 1"
            ));
            $theme = array();
            foreach ($res_theme as $k =>$v){
                $theme[$v['theme_id']] = $v['theme_title'];
            }
            $this->assign('typeList',array(
                '1'   => Advertisement_settingModel::$typeValue[1],
                '2'   => Advertisement_settingModel::$typeValue[2],
                '3'   => Advertisement_settingModel::$typeValue[3],
            ));
            $this->assign('typeIDList',array(
                '1'   => Advertisement_settingModel::$typePosition[1],
                '2'   => Advertisement_settingModel::$typePosition[2],
                '3'   => Advertisement_settingModel::$typePosition[3],
            ));
            $this->assign('closeList',array(
                '1'   => Advertisement_settingModel::$closeValue[1],
                '0'   => Advertisement_settingModel::$closeValue[0],
            ));
            $this->assign('theme', $theme);
            $this->display('advertisement_setting.form.html');
        }
        else
        {
            if(!$_POST['advertisement_title']){
                $this->show_message('请填写标题！');
                return false;
            }
            if($_FILES['advertisement_url']['error'] == 4 || $_FILES['advertisement_url']['size'] == 0){
                $this->show_message('请上传图片！');
                return false;
            }
            if($_POST['type'] == 0){
                $this->show_message('请选择类型！');
                return false;
            }
            if($_POST['type_id'] == 0){
                $this->show_message('请选择显示位置！');
                return false;
            }

            if(!$_POST['theme_id']){
                $this->show_message('请选择主题！');
                return false;
            }

            $data = array(
                'type'                => $_POST['type'],
                'theme_id'            => $_POST['theme_id'],
                'advertisement_title' => $_POST['advertisement_title'],
                'advertisement_link'  => $_POST['advertisement_link'],
                'type_id'             => $_POST['type_id'],
                'closed'              => $_POST['closed'],
            );

            $logo =   $this->_upload($_FILES,'advertisement_url');
            if(!empty($logo)){
                $data['advertisement_url'] = $logo;
            }
            if ($logo === false)
            {
                return;
            }
            if (!$this->_advertisement_mod->add($data))
            {
                $this->show_warning($this->_advertisement_mod->get_error());
                return;
            }else{
                $this->admin_log("添加[".$_POST['ad_name']."]",1);
            }

            $this->show_message('添加成功', 'back_list', 'index.php?app=advertisement_setting', 'continue_add', 'index.php?app=advertisement_setting&amp;act=add'
            );
        }
    }

    function edit(){
        $advertisement_id = $_GET['advertisement_id'];
        if(!$advertisement_id){
            $this->show_message('数据错误！');
            return false;
        }
        $find_data = $this->_advertisement_mod->get(array("conditions"=>"advertisement_id = ".$advertisement_id));
        $find_data['advertisement_url'] = WEB_PHOTO_SRC .'/'.$find_data['advertisement_url'];

        if(IS_POST){
            if(!$_POST['advertisement_title']){
                $this->show_message('请填写标题！');
                return false;
            }
            if($_POST['type'] == 0){
                $this->show_message('请选择类型！');
                return false;
            }
            if($_POST['type_id'] == 0){
                $this->show_message('请选择显示位置！');
                return false;
            }
            if(!$_POST['theme_id']){
                $this->show_message('请选择主题！');
                return false;
            }

            $data = array(
                'type'                => $_POST['type'],
                'theme_id'            => $_POST['theme_id'],
                'advertisement_title' => $_POST['advertisement_title'],
                'advertisement_link'  => $_POST['advertisement_link'],
                'type_id'             => $_POST['type_id'],
                'closed'              => $_POST['closed'],
            );
            $logo =   $this->_upload($_FILES,'advertisement_url');
            $logo && $data['advertisement_url'] = $logo;

            $rows=$this->_advertisement_mod->edit($advertisement_id, $data);
            if ($this->_advertisement_mod->has_error())
            {
                $this->show_warning($this->_advertisement_mod->get_error());
                return;
            }else{
                $this->admin_log("修改广告[".$_POST['advertisement_title']."]",1);
            }

            $this->show_message('保存成功',
                '返回列表',        'index.php?app=advertisement_setting',
                '再次编辑',    'index.php?app=advertisement_setting&amp;act=edit&amp;advertisement_id=' . $advertisement_id);
        }else{
            $res_theme = $this->_theme_setting_mod->find(array(
                "conditions" => "preview = 1"
            ));
            $theme = array();
            foreach ($res_theme as $k =>$v){
                $theme[$v['theme_id']] = $v['theme_title'];
            }
            $this->assign('typeList',array(
                '1'   => Advertisement_settingModel::$typeValue[1],
                '2'   => Advertisement_settingModel::$typeValue[2],
                '3'   => Advertisement_settingModel::$typeValue[3],
            ));
            $this->assign('typeIDList',array(
                '1'   => Advertisement_settingModel::$typePosition[1],
                '2'   => Advertisement_settingModel::$typePosition[2],
                '3'   => Advertisement_settingModel::$typePosition[3],
            ));
            $this->assign('closeList',array(
                '1'   => Advertisement_settingModel::$closeValue[1],
                '0'   => Advertisement_settingModel::$closeValue[0],
            ));
            $this->assign('theme', $theme);
            $this->assign('info',$find_data);
            $this->display('advertisement_setting.form.html');
        }
    }
    function drop(){
        $advertisement_id = isset($_GET['advertisement_id']) ? trim($_GET['advertisement_id']) : '';
        if (!$advertisement_id) {
            $this->show_warning('no_such_banner');
            return;
        }
        if (!$this->_advertisement_mod->drop($advertisement_id)) {    //删除
            $this->show_warning($this->_advertisement_mod->get_error());
            return;
        }
        $this->show_message('删除成功');
    }
    function operation(){
        $advertisement_id = $_GET['advertisement_id']?$_GET['advertisement_id']:'';
        $param = $_GET['param']?$_GET['param']:1;
        if(!$advertisement_id || !$param){
            $this->show_message('参数错误！');
            return false;
        }
        $data['closed'] = $param;
        $this->_advertisement_mod->edit($advertisement_id,$data);
        $this->show_warning('操作成功！');

    }


    function _upload($file,$str){
        require_once '../includes/aliyunOss/samples/Common.php';
        $image = $file[$str];
        $imageName = date("YmdHis", time()) . rand(1000,10000);
        $path = 'data/files/mall/advertisement_setting/'.$str;

        if ($image['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES[$str]); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=advertisement_setting&amp;act=index');
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        if (!$file_path = $uploader->save($path,$imageName)) {   //保存到指定目录，并以指定文件名$ad_id存储
            $this->show_message('上传文件失败！');
            return false;
        }
        $ossClient = new \common;
        $r = $ossClient->putObjectFile('../'.$file_path,$file_path);
        if(!$r['info'] || $r['info']['http_code'] != 200){
            $this->show_message('上传oss失败！');
            return false;
        }
        unlink('../'.$file_path);
        return $file_path;
    }

}